<?php
class Dmf_Http_Manager_Curl
{
    /**
     * Set default options form curl
     * 
     * @param cUrl $ch 
     */
    public function setDefaultOptions( &$ch )
    {
        curl_setopt( $ch, CURLOPT_HEADER, 0 );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
    }
    
    /**
     * Make a POST request to a server
     * 
     * @param string $url
     * @param array $data
     * @param array $options
     * @return Dmf_Http_Request 
     */
    public function post( $url, $data = array(), $options = array() )
    {
        $ch = curl_init();
        $query_string = '';
        
        foreach( $data as $index => $value )
        {
            $query_string .= $index . '=' . $value . '&';
        }
        
        curl_setopt( $ch, CURLOPT_POST, count( $data ) );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $query_string );
        curl_setopt( $ch, CURLOPT_URL, $url );
        
        $this->setDefaultOptions( $ch );
        
        curl_setopt_array( $ch, $options );
        
        $body = curl_exec( $ch );
        
        return new Dmf_Http_Request( $body, curl_getinfo( $ch ), curl_error( $ch ) );
    }
    
    /**
     * Make a GET request to a server
     * 
     * @param string $url
     * @param array $options
     * @return Dmf_Http_Request 
     */
    public function get( $url, $options = array() )
    {
        $ch = curl_init();
        
        curl_setopt( $ch, CURLOPT_URL, $url );
        
        $this->setDefaultOptions( $ch );
        
        curl_setopt_array( $ch, $options );
        
        $body = curl_exec( $ch );
        
        return new Dmf_Http_Request( $body, curl_getinfo( $ch ), curl_error( $ch ) );
    }
}